<?php

namespace App\Http\Controllers\Test;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Cache;

class PostController extends Controller
{
    /**
     * 显示文章
     */
    public function index()
    {
        $posts=Cache::get('posts',[]);
        var_dump($posts);
        if(!$posts){
            exit('Notiong');
        }else{
            $html='<ul>';
            foreach ($posts as $key=>$post) {
                $html.='<li><a href='.route('post.show',['post'=>$key]).'>'.$post['title'].'</a></li>';
            }
            $html.='</ul>';
            return $html;
        }

    }

    /*
     * 创建文章列表单页面
     */
    public function create()
    {
        $postUrl=route('post.store');
        $csrf_field=csrf_field();
        $html=<<<CREATE
          <form action="$postUrl" method="POST">
          $csrf_field
          标题：<input type="text" name="title"><br/><br/>
          内容：<textarea name="content" cols="50" rows="5"></textarea><br/><br/>
          <input type="submit" value="提交"/>
          </form>
CREATE;
        return $html;

    }

    /*
     * 将新创建的文章存储到存储器
     */
    public function store(Request $request)
    {
        $title=$request->input('title');
        $content=$request->input('content');
        $post=['title'=>trim($title),'content'=>trim($content)];

        $posts=Cache::get('posts',[]);

        if(!Cache::get('post_id')){
            Cache::add('post_id',1,60);
        }else{
            Cache::increment('post_id',1);
        }
        $posts[Cache::get('post_id')]=$post;
        Cache::put('posts',$posts,60);

        return redirect()->route('post.show',['post'=>Cache::get('post_id')]);
    }
    /*
     * 显示指定文章
     */
    public function show($id)
    {
        $posts=Cache::get('posts',[]);
        if(!$posts || !$posts[$id]){
            exit('Noting Found!');
        }else{
            $post=$posts[$id];
            $editUrl=route('post.edit',['post'=>$id]);
            $html=<<<DETAIL
            <h3>{$post['title']}</h3>
            <p>{$post['content']}</p>
            <p>
              <a href="{$editUrl}">编辑</a>
            </p>
DETAIL;
            return $html;
        }
    }
    /*
     * 显示编辑指定文章的表单页面
     */
    public function edit($id)
    {
        $posts=Cache::get('posts',[]);
        if(!$posts || !$posts[$id]){
            exit('Notiong Found');
        }else{
            $post=$posts[$id];
            $postUrl=route('post.update',['post'=>$id]);
            $csrf_field=csrf_field();
            $html=<<<UPDATE
            <form action="$postUrl" method="POST">
                $csrf_field
                <input type="hidden" name="_method" value="PUT">
                标题：<input type="text" name="title" value="{$post['title']}"><br/><br/>
                内容：<textarea name="content" cols="50" rows="5">{$post['content']}</textarea><br/><br/>
                <input type="submit" value="提交"/>
            </form>
UPDATE;
            return $html;

        }

    }

    /**
     * 在存储器中更新指定文章
     * @param Request $request
     * @param $id
     */
    public function update(Request $request, $id)
    {
        $posts=Cache::get('posts',[]);
        if(!$posts || !$posts[$id]){
            exit('Nothing Found!');
        }else{
            $title=$request->input('title');
            $content=$request->input('content');

            $posts[$id]['title']=trim($title);
            $posts[$id]['content']=trim($content);

            Cache::put('posts',$posts,60);
            //跳转
            return redirect()->route('post.show',['post'=>Cache::get('post_id')]);
        }

    }

    /**
     * 从存储器移除指定文章
     * @param $id
     */
    public function destroy($id)
    {
        $posts=Cache::get('posts',[]);
        if(!$posts || $posts[$id]){
            exit('Noting Deleted!');
        }else{
            unset($posts[$id]);
            Cache::decrement('post_id',1);
            return redirect()->route('post.index');
        }
    }
}
